package icasue.tools.sqlhelper.core;

import icasue.reflect.annotations.NotNull;
import icasue.tools.sqlhelper.core.SQLSplit;
import icasue.tools.sqlhelper.core.Condition;
import icasue.tools.sqlhelper.common.Const;

import java.util.LinkedList;
import java.util.List;

/**
 * @Author: Qiao Hang
 * @CreateDate: 2021/10/14 下午3:32
 * @UpdateDate:
 * @Description:
 */
public class Queries {
    private Condition _observe;
    public final String symbol;
    public final List<Condition> conditions;

    Queries(){
        // default symbol is null.
        this(null);
    }

    Queries(String symbol){
        if(symbol == null || symbol.equals(Const.AND) || symbol.equals(Const.OR)){
            this.symbol = symbol;
        }else {
            this.symbol = null;
        }
        this.conditions = new LinkedList<>();
    }


    private void build(SQLSplit.SplitOP operate, String jField, Object... values){
        if(_observe == null){
            _observe = new Condition(null);
            conditions.add(_observe);
        }
        _observe.operate = operate;
        _observe.jField = jField;
        _observe.values = values;
    }

    // open next condition with logic 'and'.
    public Queries and(){
        _observe = new Condition(Const.AND);
        conditions.add(_observe);
        return this;
    }


    // open next condition with logic 'or'.
    public Queries or(){
        _observe = new Condition(Const.OR);
        conditions.add(_observe);
        return this;
    }

    public Queries eq(@NotNull String field,@NotNull Object value){
        build(SQLSplit.SplitOP.EQ, field, value);
        return this;
    }

    public Queries gt(@NotNull String field,@NotNull Object value){
        build(SQLSplit.SplitOP.GT, field, value);
        return this;
    }

    public Queries lt(@NotNull String field,@NotNull Object value){
        build(SQLSplit.SplitOP.LT, field, value);
        return this;
    }

    public Queries gte(@NotNull String field,@NotNull Object value){
        build(SQLSplit.SplitOP.GTE, field, value);
        return this;
    }

    public Queries lte(@NotNull String field,@NotNull Object value){
        build(SQLSplit.SplitOP.LTE, field, value);
        return this;
    }

    public Queries like(@NotNull String field,@NotNull Object value){
        build(SQLSplit.SplitOP.LIKE, field, value);
        return this;
    }

    public Queries in(@NotNull String field,@NotNull Object... ranges){
        build(SQLSplit.SplitOP.IN, field, ranges);
        return this;
    }

    public Queries notIn(@NotNull String field,@NotNull Object... ranges){
        build(SQLSplit.SplitOP.NOT_IN, field, ranges);
        return this;
    }

    public Queries between(@NotNull String field, @NotNull Object... left_right){
        build(SQLSplit.SplitOP.BETWEEN, field, left_right);
        return this;
    }

    public Queries isNull(@NotNull String field){
        build(SQLSplit.SplitOP.IS_NULL, field, null);
        return this;
    }

    public Queries isNotNull(@NotNull String field){
        build(SQLSplit.SplitOP.IS_NOT_NULL, field, null);
        return this;
    }
}
